/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.service.OfficeService;
import com.eit.eitsite.modules.sys.service.SystemService;
import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.zhuoyue.elmt.dao.XtPqxqgxDao;
import com.zhuoyue.elmt.entity.*;
import com.zhuoyue.elmt.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 设备分组Controller
 *
 * @author JiangYH
 * @version 2017-02-09
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/xtPq")
public class XtPqController extends BaseController {

    @Autowired
    private DeviceParametersService deviceParametersService;

    @Autowired
    private MaintenanceStaffService maintenanceStaffService;

    @Autowired
    private AdministrativeAreasService administrativeAreasService;

    @Autowired
    private WbMtGroupService wbMtGroupService;

    @Autowired
    private XtPqService xtPqService;

    @Autowired
    private XtPqxqgxService xtPqxqgxService;

    @ModelAttribute
    public XtPq get(@RequestParam(required = false) String id) {
        XtPq entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = xtPqService.get(id);
        }
        if (entity == null) {
            entity = new XtPq();
        }
        return entity;
    }

//    @RequiresPermissions("elmt:xtPq:view")
//    @RequestMapping(value = {"list", ""})
//    public String list(XtPq xtPq, HttpServletRequest request, HttpServletResponse response, Model model) {
//        xtPq.setMaintenUnitCode(UserUtils.getUser().getCompany().getCode());
//        Page<XtPq> page = xtPqService.findPage(new Page<XtPq>(request, response), xtPq);
//        model.addAttribute("page", page);
//        return "zhuoyue/elmt/xtPqList";
//    }

    @RequiresPermissions("elmt:xtPq:view")
    @RequestMapping(value = {"list", ""})
    public String listWb(XtPq xtPq, HttpServletRequest request, HttpServletResponse response, Model model) {
        HashMap map =new HashMap();
        map.put("loginName",UserUtils.getUser().getLoginName());
        List<HashMap> list= xtPqService.findWbList(map);
        model.addAttribute("list", list);
        return "zhuoyue/elmt/xtPqListWb";
    }

    @RequiresPermissions("elmt:xtPq:view")
    @RequestMapping(value = "form")
    public String form(XtPq xtPq, Model model) {
        model.addAttribute("xqList", administrativeAreasService.getAll());

        WbMtGroup qryWbMtGroup = new WbMtGroup();
        qryWbMtGroup.setMaintenUnitCode(UserUtils.getUser().getCompany().getCode());
        model.addAttribute("groupList", wbMtGroupService.findList(qryWbMtGroup));

        MaintenanceStaff maintenanceStaff = new MaintenanceStaff();
        maintenanceStaff.setMaintenanceUnitId(UserUtils.getUser().getCompany().getCode());
        List<MaintenanceStaff> staffList = maintenanceStaffService.findList(maintenanceStaff);
        model.addAttribute("staffList", staffList);

        // 拿到设备分组对应的小组、人员、辖区、设备
        if (StringUtils.isNotEmpty(xtPq.getId())) {
            XtPqxqgx qryXtPqxqgx = new XtPqxqgx();
            qryXtPqxqgx.setPqId(xtPq.getId());

            qryXtPqxqgx.setRemark("1");
            List<XtPqxqgx> groupXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
            xtPq.setGroupList(groupXtPqxqgxList);

            qryXtPqxqgx.setRemark("2");
            List<XtPqxqgx> staffXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
            xtPq.setStaffList(staffXtPqxqgxList);

            qryXtPqxqgx.setRemark("3");
            List<XtPqxqgx> xqXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
            xtPq.setXqList(xqXtPqxqgxList);

            qryXtPqxqgx.setRemark("4");
            List<XtPqxqgx> sbXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
            xtPq.setSbList(sbXtPqxqgxList);
        }

        model.addAttribute("xtPq", xtPq);

        return "zhuoyue/elmt/xtPqForm";
    }

    @RequiresPermissions("elmt:xtPq:edit")
    @RequestMapping(value = "save")
    public String save(XtPq xtPq, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, xtPq)) {
            return form(xtPq, model);
        }

        xtPq.setMaintenUnitCode(UserUtils.getUser().getCompany().getCode());
        xtPqService.savePq(xtPq);

        addMessage(redirectAttributes, "保存设备分组成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/xtPq/?repage";
    }

    @RequiresPermissions("elmt:xtPq:edit")
    @RequestMapping(value = "delete")
    public String delete(XtPq xtPq, RedirectAttributes redirectAttributes) {
        xtPqService.delete(xtPq);
        addMessage(redirectAttributes, "删除设备分组成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/xtPq/?repage";
    }

    @RequiresPermissions("elmt:xtPq:view")
    @RequestMapping(value = "xtPqTreeForm")
    public String xtPqTreeForm(XtPq xtPq, Model model) {

        XtPqxqgx qryXtPqxqgx = new XtPqxqgx();
        qryXtPqxqgx.setPqId(xtPq.getPqId());
        String xqXtPqxqgxList = xtPqxqgxService.findXqList(qryXtPqxqgx);
        model.addAttribute("pqId",xtPq.getPqId());
        //所有辖区
        List<AdministrativeAreas>  xqList= administrativeAreasService.getAll();
        model.addAttribute("xqList", xqList);
        model.addAttribute("xqXtPqxq", xqXtPqxqgxList);
        return "zhuoyue/elmt/xtPqTreeForm";
    }

    // 跳转到选择设备页面
    @RequestMapping(value = {"mainXtPqSbIndex"})
    public String mainXtPqSbIndex(String pqId, XtPq xtPq, DeviceParameters deviceParameters,HttpServletRequest request, HttpServletResponse response, Model model) {

        //所有辖区
//        XtPqxqgx qryXtPqxqgx = new XtPqxqgx();
//        qryXtPqxqgx.setPqId(pqId);
//        qryXtPqxqgx.setRemark("3");
//        List<XtPqxqgx> xqXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
//        xtPq.setXqList(xqXtPqxqgxList);
//        model.addAttribute("defaultXq",xtPq );

//        List<AdministrativeAreas>  xqList= administrativeAreasService.getAll();
//        model.addAttribute("xqList", xqList);

        //设备查询
        //DeviceParameters deviceParameters=new DeviceParameters();

//        if (!UserUtils.getUser().isAdmin()) {
//            deviceParameters.setWbdwdm(UserUtils.getUser().getCompany().getCode());
//        }else {
//            if(StringUtils.isNotEmpty(xtPq.getWbdwdm())) {
//                deviceParameters.setWbdwdm(xtPq.getWbdwdm());
//            }
//        }
//        deviceParameters.setPqId(pqId);
//        deviceParameters.setGroupId(pqId);
//        if(StringUtils.isNotEmpty(xtPq.getWbdwmc())) {
//            deviceParameters.setWbdwmc(xtPq.getWbdwmc());
//        }
//        if(StringUtils.isNotEmpty(xtPq.getSbzcdm())) {
//            deviceParameters.setSbzcdm(xtPq.getSbzcdm());
//        }
//        //设备品种名称
//        if(StringUtils.isNotEmpty(xtPq.getSbpzmc())) {
//            deviceParameters.setSbpzmc(xtPq.getSbpzmc());
//        }
//        //设备使用区域
////        if(StringUtils.isNotEmpty(xtPq.getXqIds())) {
////            deviceParameters.setSbsyqy(xtPq.getXqIds());
////        }
        model.addAttribute("pqId",pqId);
//        model.addAttribute("groupId",pqId);
//        Page<DeviceParameters> setPage= new Page<DeviceParameters>(request, response);
//        setPage.setPageSize(40);
//        Page<DeviceParameters> page = deviceParametersService.xtpqsbpage(setPage,deviceParameters);
//        model.addAttribute("page", page);

        return "zhuoyue/elmt/mainXtPqSbIndex";
    }
    //区域保存设备配置
    @RequiresPermissions("elmt:xtPq:edit")
    @RequestMapping(value = "xtpqxqsave")
    public String xtPqXqSave(XtPq xtPq,Model model, RedirectAttributes redirectAttributes) {

        if(StringUtils.isNotEmpty(xtPq.getPqId())) {
            xtPqService.xtPqXqSave(xtPq);
            addMessage(redirectAttributes, "设备分配成功!");
        }else{
            addMessage(redirectAttributes, "设备分配失败！");
        }
        return "redirect:" + Global.getAdminPath() + "/elmt/xtPq/xtPqTreeForm?repage&pqId="+xtPq.getPqId();
    }

    // 待分组设备
    @RequestMapping(value = {"sblist"})
    public String sblist(String pqId, DeviceParameters deviceParameters, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (!UserUtils.getUser().isAdmin()) {
            deviceParameters.setWbdwdm(UserUtils.getUser().getCompany().getCode());
        }
//        XtPq xtPq = xtPqService.get(pqId);

//        HashMap qryMap = new HashMap();
//        qryMap.put("pqId", pqId);
//        qryMap.put("wbdwdm", UserUtils.getUser().getCompany().getCode());
//        qryMap.put("groupId", xtPq.getGroupIds());
//        qryMap.put("staffId", xtPq.getStaffIds());
          deviceParameters.setPqId(pqId);
          deviceParameters.setGroupId(pqId);
//          deviceParameters.setStaffId(xtPq.getStaffIds());

         model.addAttribute("pqId", pqId);
         model.addAttribute("groupId", pqId);
//        model.addAttribute("staffId", xtPq.getStaffIds());

        Page<DeviceParameters> page = deviceParametersService.xtpqsbpage(new Page<DeviceParameters>(request, response), deviceParameters);
        model.addAttribute("page", page);

        //List<DeviceParameters> xtpqsbnolist = deviceParametersService.xtpqsbnolist(qryMap);
        //model.addAttribute("xtpqsbnolist", xtpqsbnolist);

        return "zhuoyue/elmt/mainXtPqSb";
    }

    // 已选择设备
    @RequestMapping(value = {"sbnolist"})
    public String sbnolist(String pqId,String groupType, DeviceParameters deviceParameters, HttpServletRequest request, HttpServletResponse response, Model model) {

        HashMap qryMap = new HashMap();
        qryMap.put("pqId", pqId);
        qryMap.put("wbdwdm", UserUtils.getUser().getCompany().getCode());
        qryMap.put("groupId", pqId);
        //qryMap.put("tzsbbh", 1);
        //分组类型 1：显示按区域和设备分组全部，2：按设备分组，3：按合同分组
        qryMap.put("groupType", groupType);

        if(StringUtils.isNotEmpty(deviceParameters.getWbdwmc())) {
            qryMap.put("wbdwmc", deviceParameters.getWbdwmc());
        }
        //设备注册代码
        if(StringUtils.isNotEmpty(deviceParameters.getSbzcdm())) {
            qryMap.put("sbzcdm", deviceParameters.getSbzcdm());
        }
        //设备品种名称
        if(StringUtils.isNotEmpty(deviceParameters.getSbpzmc())) {
            qryMap.put("sbpzmc", deviceParameters.getSbpzmc());
        }
        //qryMap.put("staffId", xtPq.getStaffIds());


        model.addAttribute("pqId", pqId);
        model.addAttribute("groupId",pqId);
        //model.addAttribute("staffId", xtPq.getStaffIds());

        //Page<DeviceParameters> page = deviceParametersService.xtpqsbpage(new Page<DeviceParameters>(request, response), deviceParameters);
        //model.addAttribute("page", page);

        List<DeviceParameters> xtpqsbnolist = deviceParametersService.xtpqsbnolist(qryMap);
        model.addAttribute("xtpqsbnolist", xtpqsbnolist);

        return "zhuoyue/elmt/mainXtPqNoSb";
    }

    //设备保存设备配置
    @RequestMapping(value = "xtpqsbsave")
    public String xtpqsbsave(String[] tzsbbh, String pqId, Model model, RedirectAttributes redirectAttributes) {
//        XtPq xtPq = xtPqService.get(pqId);

//        XtPqxqgx qryXtPqxqgx = new XtPqxqgx();
//        //String groupId = "";
//        //String staffId = "";
//
//        qryXtPqxqgx.setRemark("1");
//        List<XtPqxqgx> groupXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
//        if (groupXtPqxqgxList != null && groupXtPqxqgxList.size() > 0) {
//            groupId = groupXtPqxqgxList.get(0).getGroupId();
//        }
//
//        qryXtPqxqgx.setRemark("2");
//        List<XtPqxqgx> staffXtPqxqgxList = xtPqxqgxService.findList(qryXtPqxqgx);
//        if (groupXtPqxqgxList != null && groupXtPqxqgxList.size() > 0) {
//            staffId = staffXtPqxqgxList.get(0).getStaffId();
//        }

        XtPqxqgx xtPqxqgx;
        List<XtPqxqgx> xtPqxqgxList = new ArrayList();
        for (int i = 0; i < tzsbbh.length; i++) {
            xtPqxqgx = new XtPqxqgx();
            xtPqxqgx.setPqId(pqId);
            xtPqxqgx.setGroupId(pqId);
            //xtPqxqgx.setStaffId(staffId);
            xtPqxqgx.setSbId(tzsbbh[i]);
            xtPqxqgxList.add(xtPqxqgx);
        }
        xtPqxqgxService.saveUpdateDevice(new XtPq(), xtPqxqgxList);
        addMessage(redirectAttributes, "设备分配成功!");
        //model.addAttribute("message", "设备分配成功!");
        return "redirect:" + Global.getAdminPath() + "/elmt/xtPq/sblist?repage&pqId="+pqId;
        //return "redirect:zhuoyue/elmt/xtPq/sblist?pqId=" + pqId;
    }

    @RequiresPermissions("elmt:xtPq:edit")
    @RequestMapping(value = "xtpqsbdelete")
    public String xtpqsbdelete(String[] tzsbbh,String pqId, Model model, RedirectAttributes redirectAttributes) {
        //XtPq xtPq = xtPqService.get(pqId);
        XtPqxqgx xtPqxqgx;
        List<XtPqxqgx> xtPqxqgxList = new ArrayList();
        for (int i = 0; i < tzsbbh.length; i++) {
            xtPqxqgx = new XtPqxqgx();
            xtPqxqgx.setPqId(pqId);
            xtPqxqgx.setGroupId(pqId);
            xtPqxqgx.setSbId(tzsbbh[i]);
            xtPqxqgxList.add(xtPqxqgx);
        }
        xtPqxqgxService.deleterela(xtPqxqgxList);

        //model.addAttribute("message", "删除已分配设备成功");
        addMessage(redirectAttributes, "删除已分配设备成功!");
        return "redirect:" + Global.getAdminPath() + "/elmt/xtPq/sbnolist?repage&pqId="+pqId;
    }
}